var util = require('util');

app.get('/link/:id/remove', function(req, res) {
	model.Link.remove({
		_id : req.params.id
	}, function(e) {
		if (e)
			log.error(e, 'Controls/Links:8');
		log.debug("Remove Link " + req.params.id);
		res.redirect('https://' + req.header('X-Request-Host') + '/link');
	});
});

app.get('/link/add', function(req, res) {
	res.local('title', 'Add Link');
	log.debug("Add Link");
	model.Port.find({}, function(e, ports) {
		res.local('ports', ports);
		res.local('link', {inputs : {}});
		res.local('action', 'add');
		res.render('Link/edit.jade', {
			pretty : true
		});
	});
});

app.get('/link/:id/edit', function(req, res) {
	res.local('title', 'Edit Link');
	model.Link.findById(req.params.id, function(e) {
		if (e)
			log.error(e, 'Controls/Links:31');
	}).populate('inputs').populate('output').run(
			function(e, link) {
				if (e)
					log.error(e, 'Controls/Links:35');
				if (link) {
					model.Port.find({}, function(e, ports) {
						log.debug('Found Link. Edit');
						res.local('link', link);
						res.local('ports', ports);
						res.local('action', 'update');
						res.render('Link/edit.jade', {
							pretty : true
						});
					});
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/link');
				}
			});
});

app.get('/link/:id', function(req, res) {
	res.local('title', 'Link');
	model.Link.findById(req.params.id, function(e) {
		if (e)
			log.error(e, 'Controls/Links:57');
	}).populate('inputs')
	.populate('output')
	.run(function(e, link) {
				if (e)
					log.error(e, 'Controls/Links:62');
				if (link) {
					log.debug('Found Link');
					res.local('link', link);
					res.render('Link/view.jade', {
						pretty : true
					});
				} else {
					res.redirect('https://' + req.header('X-Request-Host')
							+ '/link');
				}
			});
});

app.get('/link', function(req, res) {
	model.Link.find({}, function(e, links) {
		if (e)
			log.error(e, 'Controls/Links:79');
		res.local('title', 'Links');
		res.local('links', links);
		res.render('Link/list.jade');
	});
});

app.post('/link', function(req, res) {
	log.debug('Change Link');

	if (typeof (req.body.action) != 'undefined') {
		log.debug('Action: ' + req.body.action);
		switch (req.body.action) {
		case 'add':
			var l = new model.Link({
				name : req.body.name,
				output : req.body.output,
				inputs : req.body.inputs,
				description : req.body.description,
				relation : req.body.relation
			});
			
			l.save(function(e) {
				if (e)
					log.error(e, 'Controls/Links:103');
				res.redirect('https://' + req.header('X-Request-Host')
						+ '/link/' + l._id);
			});
			break;
		case 'update':
			model.Link.update({
				_id : req.body._id
			}, {
				name : req.body.name,
				inputs : req.body.inputs,
				output : req.body.output,
				description : req.body.description,
				relation : req.body.relation
			}, function(e) {
				if (e)
					log.error(e, 'Controls/Links:119');

				res.redirect('https://' + req.header('X-Request-Host')
						+ '/link/' + req.body._id);
			});
			break;
		default:
			res.redirect('https://' + req.header('X-Request-Host') + '/link/'
					+ req.body._id);
		}
	}
});
